package com.coocaa.x.framework.utils;

import com.coocaa.x.framework.data.JObject;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.service.litedb.data.client.LiteDBSupport;
import com.tianci.media.api.Log;

import org.apache.http4.HttpResponse;
import org.apache.http4.HttpStatus;
import org.apache.http4.client.HttpClient;
import org.apache.http4.client.methods.HttpGet;
import org.apache.http4.impl.client.DefaultHttpClient;
import org.apache.http4.util.EntityUtils;

import java.util.List;


/**
 * Created by admin on 2015/12/8.
 */
public class FansChannelUpgrade {
    private static final long MONTH = 30*24*60*60;
//    private static final long MONTH = 60 * 5;   //test
    private static final int DOWNLOADCOUNTS = 10;
    private static final int OPENSTORETIMES = 20;
    private static final String FANS_FLAG = "fans_flag";

    public static class PojieSwitchData extends JObject{
        public String ret;
        public boolean data;
        public String msg;

        public PojieSwitchData() {
        }
    }

    public static class DownloadAppData extends LiteDBSupport {
        public String name;
        public long downloadTime;

        public DownloadAppData() throws Exception {
            super("download_app", 1);
        }
    }

    public static class OpenAPPSTOREData extends LiteDBSupport{
        public long openTime;

        public OpenAPPSTOREData() throws Exception {
            super("open_store", 1);
        }
    }

    public static synchronized boolean insertDownloadData(String name,long time) {
//        boolean downloadOK = getDownloadcounts();
        boolean downloadOK = getFansFlag();
        LogUtils.d("fans","insertDownloadData  downloadOK:"+downloadOK);
        if(downloadOK)
            return true;
        try {
            DownloadAppData data = new DownloadAppData();
            long temp = time - MONTH;
            LogUtils.d("fans","insertDownloadData  temp:"+temp);
            data.delete(" downloadTime < ?", String.valueOf(temp));

            data.name = name;
            data.downloadTime = time;
            data.insert();

        } catch (Exception e) {
            LogUtils.d("fans", "insertDownloadData error ...");
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean getDownloadcounts()
    {
        try {
            DownloadAppData data = new DownloadAppData();
            List<LiteDBSupport> list = data.selectAll();
            if(list != null) {
                for(LiteDBSupport db : list)
                {
                    DownloadAppData d = (DownloadAppData)db;
                    LogUtils.d("fans","-------****name:"+d.name+" time:"+d.downloadTime);
                }
                LogUtils.d("fans","getDownloadcounts  list.size():"+list.size());
                if(list.size() >= DOWNLOADCOUNTS)
                    return true;
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static synchronized boolean insertOpenTimes(long time){
//        boolean openTimesOK = getOpenstoretimes();
        boolean openTimesOK = getFansFlag();
        LogUtils.d("fans","insertOpenTimes  openTimesOK:"+openTimesOK);
        if(openTimesOK)
            return true;
        try {
            OpenAPPSTOREData data = new OpenAPPSTOREData();
            long temp = time - MONTH;
            data.delete(" openTime < ? ", String.valueOf(temp));

            data.openTime = time;
            data.insert();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean getOpenstoretimes(){
        try {
            OpenAPPSTOREData data = new OpenAPPSTOREData();
            List<LiteDBSupport> list = data.selectAll();
            if(list != null) {
                for(LiteDBSupport db : list)
                {
                    OpenAPPSTOREData d = (OpenAPPSTOREData)db;
                    LogUtils.d("fans","-------**** time:"+d.openTime);
                }
                LogUtils.d("fans","getOpenstoretimes  list.size():"+list.size());
                if(list.size() >= OPENSTORETIMES)
                    return true;
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setFansFlag(boolean flag){
        try {
            XServiceManager.getLiteConfigService().saveBooleanConfig(FANS_FLAG, flag);
            return true;
        }catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean getFansFlag(){
        try {
            boolean flag = XServiceManager.getLiteConfigService().getBooleanValue(FANS_FLAG,false);
            return flag;
        }catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static String getPojieSwitchFromWeb (String server) {
        String httpUrl = "http://"+server + "/index.html?ctype=yyqPojieSwitch";
        LogUtils.d("server","getJsonFromWeb  httpUrl:"+httpUrl);
        String strResult = "";
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet(httpUrl);
        try{
            // 取得HttpClient对象
//            HttpClient httpclient =  HttpConnectionManager.getHttpClient();
            HttpClient httpclient =  new DefaultHttpClient();
//            httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,10000);
            // 请求HttpClient，取得HttpResponse
            HttpResponse httpResponse = httpclient.execute(httpRequest);

            int statCode = httpResponse.getStatusLine().getStatusCode();
            // 请求成功
            if (statCode == HttpStatus.SC_OK) {
                // 取得返回的字符串
                strResult = EntityUtils.toString(httpResponse.getEntity());
            } else {
                LogUtils.d("server","HttpClient请求"+httpUrl+"出错，错误代码:" + statCode);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //释放http连接
            httpRequest.releaseConnection();
        }
        LogUtils.d("server","getJsonFromWeb  strResult:"+strResult);
        return strResult;
    }

}
